package cn.shyouchi.wuguanjia.android.ui.attendance.temporary;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.controls.AttendanceResDialog;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.attendance.AttendanceTokePhotoActivity;
import cn.shyouchi.wuguanjia.android.ui.attendance.BaseAttendanceActivity;
import cn.shyouchi.wuguanjia.android.ui.event.temporary.PreviewTemporaryActivity;
import cn.shyouchi.wuguanjia.android.ui.event.temporary.TemporaryEntity;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class StaffTemporaryActivity extends BaseAttendanceActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_location)
    TextView mTvLocation;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.btn_sign_out)
    LinearLayout mBtnOut;
    @BindView(R.id.btn_sign_in)
    LinearLayout mBtnIn;


    @BindView(R.id.tv_content)
    LinearLayout mTvContent;
    @BindView(R.id.img_btn_flag)
    ImageView mImgBtnFlag;
    @BindView(R.id.activity_map)
    LinearLayout mActivityMap;

    String addr = "";

    boolean locationSuc = false;

    AttendanceApi mAttendanceApi;

    TemporaryEntity.ResultMessageBean task;


    private final int REQUEST_CODE_SIGN_IN = 10010;
    private final int REQUEST_CODE_SIGN_OUT = 10011;
    @BindView(R.id.tv_task_time)
    TextView mTvTaskTime;
    @BindView(R.id.tv_task_addr)
    TextView mTvTaskAddr;
    @BindView(R.id.tv_task_duration)
    TextView mTvTaskDuration;


    @Override
    public int initContentView() {
        return R.layout.activity_staff_temporary;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        task = getIntent().getParcelableExtra("task");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        if (task != null) {
            initFloatWin();
        }

    }

    private void initFloatWin() {
        mTvTaskTime.setText(String.format("工作时间：%1s - %2s", task.getStartingtime().substring(0, 16), task.getEndtime().substring(0, 16)));
        mTvTaskAddr.setText(String.format("任务名称：%1s", task.getAddress()));
        mTvTaskDuration.setText(String.format("工作时长：%1s小时", String.valueOf(task.getTime())));
    }

    @Override
    public int initMapView() {
        return R.id.map;
    }

    @Override
    public void onTimeChange(long date) {

    }


    @OnClick(R.id.img_btn_flag)
    public void onFlagClick() {
        mTvContent.setVisibility(mTvContent.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
        mImgBtnFlag.setImageResource(mTvContent.getVisibility() == View.GONE ? R.mipmap.ic_float_win_forward : R.mipmap.ic_float_win_back);
    }


    @Override
    public void onLocationChanged(AMapLocation location) {
        super.onLocationChanged(location);
        addr = location.getAddress();
        mTvLocation.setText(addr);
    }


    @Override
    public void onLocationStart() {
        super.onLocationStart();
        mTvLocation.setText("正在定位...");
    }

    @Override
    public void onLocationStop(boolean success) {
        super.onLocationStop(success);
        locationSuc = success;
        if (!success) {
            mTvLocation.setText("定位失败");
        }
    }


    @OnClick({R.id.btn_sign_in, R.id.btn_sign_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sign_in:
                startActivityForResult(new Intent(mContext, AttendanceTokePhotoActivity.class), REQUEST_CODE_SIGN_IN);
                break;
            case R.id.btn_sign_out:
                startActivityForResult(new Intent(mContext, AttendanceTokePhotoActivity.class), REQUEST_CODE_SIGN_OUT);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SIGN_IN && resultCode == AttendanceTokePhotoActivity.RESULT_CODE) {
            String path = data.getStringExtra("path");
            sign(path, 1);
        } else if (requestCode == REQUEST_CODE_SIGN_OUT && resultCode == AttendanceTokePhotoActivity.RESULT_CODE) {
            String path = data.getStringExtra("path");
            sign(path, 2);
        }
    }


    private void sign(String path, int t) {
        mAttendanceApi.temporarySignIn(String.valueOf(task.getId()), String.valueOf(t), addr, path)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        AttendanceResDialog.newInstance("签到结果", "签到成功！", String.valueOf(0)).show(getFragmentManager(), "signin");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        AttendanceResDialog.newInstance("签到结果", "签到失败！\n" + e.getMessage(), String.valueOf(1)).show(getFragmentManager(), "signin");
                    }

                    @Override
                    public void onNext(BaseRequestResult result) {

                    }
                });
    }

    @OnClick(R.id.tv_check_info)
    public void onClick() {
        Intent intent = new Intent(mContext, PreviewTemporaryActivity.class);
        intent.putExtra("temporary", task);
        startActivity(intent);
    }
}
